home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / amiga / asrc29k.lha / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-08  |  3.6 KB  |  211 lines

  1. /* Miscellaneous machine independent utilities */
  2. #include <stdio.h>
  3. #include "global.h"
  4. #include "socket.h"
  5. #include "mbuf.h"
  6.  
  7. /* Select from an array of strings, or return ascii number if out of range */
  8. char *
  9. smsg(msgs,nmsgs,n)
  10. char *msgs[];
  11. unsigned nmsgs,n;
  12. {
  13.     static char buf[16];
  14.  
  15.     if(n < nmsgs && msgs[n] != NULLCHAR)
  16.         return msgs[n];
  17.     sprintf(buf,"%u",n);
  18.     return buf;
  19. }
  20.  
  21. /* Convert hex-ascii to integer */
  22. int
  23. htoi(s)
  24. char *s;
  25. {
  26.     int i = 0;
  27.     char c;
  28.  
  29.     while((c = *s++) != '\0'){
  30.         if(c == 'x')
  31.             continue;    /* allow 0x notation */
  32.         if('0' <= c && c <= '9')
  33.             i = (i * 16) + (c - '0');
  34.         else if('a' <= c && c <= 'f')
  35.             i = (i * 16) + (c - 'a' + 10);
  36.         else if('A' <= c && c <= 'F')
  37.             i = (i * 16) + (c - 'A' + 10);
  38.         else
  39.             break;
  40.     }
  41.     return i;
  42. }
  43. /* replace terminating end of line marker(s) with null */
  44. void
  45. rip(s)
  46. register char *s;
  47. {
  48.     register char *cp;
  49.  
  50.     if((cp = strchr(s,'\n')) != NULLCHAR)
  51.         *cp = '\0';
  52. }
  53.  
  54. /* Routines not needed for Turbo 2.0, but available for older libraries */
  55. #ifdef    AZTEC
  56.  
  57. /* Copy a string to a malloc'ed buffer */
  58. char *
  59. strdup(s)
  60. const char *s;
  61. {
  62.     register char *out;
  63.     register int len;
  64.  
  65.     if(s == NULLCHAR)
  66.         return NULLCHAR;
  67.     len = strlen(s);
  68.     out = mallocw(len+1);
  69.     /* This is probably a tad faster than strcpy, since we know the len */
  70.     memcpy(out,s,len);
  71.     out[len] = '\0';
  72.     return out;
  73. }
  74.  
  75. /* Case-insensitive string comparison */
  76. strnicmp(a,b,n)
  77. register char *a,*b;
  78. register int n;
  79. {
  80.     char a1,b1;
  81.  
  82.     while(n-- != 0 && (a1 = *a++) != '\0' && (b1 = *b++) != '\0'){
  83.         if(a1 == b1)
  84.             continue;    /* No need to convert */
  85.         a1 = tolower(a1);
  86.         b1 = tolower(b1);
  87.         if(a1 == b1)
  88.             continue;    /* NOW they match! */
  89.         if(a1 > b1)
  90.             return 1;
  91.         if(a1 < b1)
  92.             return -1;
  93.     }
  94.     return 0;
  95. }
  96.  
  97. char *
  98. strtok(s1,s2)
  99. char *s1;    /* Source string (first call) or NULL */
  100. #ifdef    __STDC__    /* Ugly kludge for aztec's declaration */
  101. const char *s2;    /* Delimiter string */
  102. #else
  103. char *s2;    /* Delimiter string */
  104. #endif
  105. {
  106.     static int isdelim();
  107.     static char *next;
  108.     register char *cp;
  109.     char *tmp;
  110.  
  111.     if(s2 == NULLCHAR)
  112.         return NULLCHAR;    /* Must give delimiter string */
  113.  
  114.     if(s1 != NULLCHAR)
  115.         next = s1;        /* First call */
  116.  
  117.     if(next == NULLCHAR)
  118.         return NULLCHAR;    /* No more */
  119.  
  120.     /* Find beginning of this token */
  121.     for(cp = next;*cp != '\0' && isdelim(*cp,s2);cp++)
  122.         ;
  123.  
  124.     if(*cp == '\0')
  125.         return NULLCHAR;    /* Trailing delimiters, no token */
  126.  
  127.     /* Save the beginning of this token, and find its end */
  128.     tmp = cp;
  129.     next = NULLCHAR;    /* In case we don't find another delim */
  130.     for(;*cp != '\0';cp++){
  131.         if(isdelim(*cp,s2)){
  132.             *cp = '\0';
  133.             next = cp + 1;    /* Next call will begin here */
  134.             break;
  135.         }
  136.     }
  137.     return tmp;
  138. }
  139. static int
  140. isdelim(c,delim)
  141. char c;
  142. register char *delim;
  143. {
  144.     char d;
  145.  
  146.     while((d = *delim++) != '\0'){
  147.         if(c == d)
  148.             return 1;
  149.     }
  150.     return 0;
  151. }
  152. #endif    /* AZTEC */
  153.  
  154.  
  155.  
  156. /* Host-network conversion routines, replaced on the 8086 with assembler */
  157. #ifndef    MSDOS
  158. /* Put a long in host order into a char array in network order */
  159. char *
  160. put32(cp,x)
  161. register char *cp;
  162. int32 x;
  163. {
  164.     *cp++ = x >> 24;
  165.     *cp++ = x >> 16;
  166.     *cp++ = x >> 8;
  167.     *cp++ = x;
  168.     return cp;
  169. }
  170. /* Put a short in host order into a char array in network order */
  171. char *
  172. put16(cp,x)
  173. register char *cp;
  174. int16 x;
  175. {
  176.     *cp++ = x >> 8;
  177.     *cp++ = x;
  178.  
  179.     return cp;
  180. }
  181. int16
  182. get16(cp)
  183. register char *cp;
  184. {
  185.     register int16 x;
  186.  
  187.     x = uchar(*cp++);
  188.     x <<= 8;
  189.     x |= uchar(*cp);
  190.     return x;
  191. }
  192. /* Machine-independent, alignment insensitive network-to-host long conversion */
  193. int32
  194. get32(cp)
  195. register char *cp;
  196. {
  197.     int32 rval;
  198.  
  199.     rval = uchar(*cp++);
  200.     rval <<= 8;
  201.     rval |= uchar(*cp++);
  202.     rval <<= 8;
  203.     rval |= uchar(*cp++);
  204.     rval <<= 8;
  205.     rval |= uchar(*cp);
  206.  
  207.     return rval;
  208. }
  209. #endif
  210.  
  211.